package com.ktgj.service.service.impl;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ktgj.common.core.constant.FileConstants;
import com.ktgj.common.core.text.UUID;
import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.service.domain.VipImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ktgj.service.mapper.ViphallAttachmentMapper;
import com.ktgj.service.domain.ViphallAttachment;
import com.ktgj.service.service.IViphallAttachmentService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 贵宾厅附件（厅图片）Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-09-03
 */
@Service
public class ViphallAttachmentServiceImpl implements IViphallAttachmentService 
{
    @Autowired
    private ViphallAttachmentMapper viphallAttachmentMapper;

    /**
     * 查询贵宾厅附件（厅图片）
     * 
     * @param linkId 贵宾厅附件（厅图片）ID
     * @return 贵宾厅附件（厅图片）
     */
    @Override
    public ViphallAttachment selectViphallAttachmentById(Long linkId)
    {
        return viphallAttachmentMapper.selectViphallAttachmentById(linkId);
    }

    /**
     * 查询贵宾厅附件（厅图片）列表
     * 
     * @param viphallAttachment 贵宾厅附件（厅图片）
     * @return 贵宾厅附件（厅图片）
     */
    @Override
    public List<ViphallAttachment> selectViphallAttachmentList(ViphallAttachment viphallAttachment)
    {
        return viphallAttachmentMapper.selectViphallAttachmentList(viphallAttachment);
    }

    /**
     * 新增贵宾厅附件（厅图片）
     * 
     * @param viphallAttachment 贵宾厅附件（厅图片）
     * @return 结果
     */
    @Override
    public int insertViphallAttachment(ViphallAttachment viphallAttachment)
    {
        viphallAttachment.setCreateTime(DateUtils.getNowDate());
        return viphallAttachmentMapper.insertViphallAttachment(viphallAttachment);
    }

    /**
     * 修改贵宾厅附件（厅图片）
     * 
     * @param viphallAttachment 贵宾厅附件（厅图片）
     * @return 结果
     */
    @Override
    public int updateViphallAttachment(ViphallAttachment viphallAttachment)
    {
        viphallAttachment.setUpdateTime(DateUtils.getNowDate());
        return viphallAttachmentMapper.updateViphallAttachment(viphallAttachment);
    }

    /**
     * 批量删除贵宾厅附件（厅图片）
     * 
     * @param linkIds 需要删除的贵宾厅附件（厅图片）ID
     * @return 结果
     */
    @Override
    public int deleteViphallAttachmentByIds(Long[] linkIds)
    {
        return viphallAttachmentMapper.deleteViphallAttachmentByIds(linkIds);
    }

    /**
     * 删除贵宾厅附件（厅图片）信息
     * 
     * @param linkId 贵宾厅附件（厅图片）ID
     * @return 结果
     */
    @Override
    public int deleteViphallAttachmentById(Long linkId)
    {
        return viphallAttachmentMapper.deleteViphallAttachmentById(linkId);
    }

    @Value("${file.save-path}")
    private String baseFilePath;

    /**
     * 上传单张贵宾厅图片
     * @param file
     * @param request
     * @return
     */
    @Override
    public VipImage importImg(MultipartFile file, HttpServletRequest request) {
        VipImage vipImage = new VipImage();
        if (file.isEmpty()) {
            vipImage.setLegal(false);
            return vipImage;
        }
        SimpleDateFormat simpleDateFormat  = new SimpleDateFormat("yyyy-MM-dd/");
        String format = simpleDateFormat.format(new Date());
        File file1 = new File(baseFilePath + format);
        if (!file1.isDirectory()) {
            file1.mkdirs();
        }
        String oldName = file.getOriginalFilename();
        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length());
        try {
            File newFile = new File(file1.getAbsolutePath() + File.separator + newName);
            file.transferTo(newFile);
            String filePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/picbook/" + FileConstants.UPLOAD_PATH_PREFIX + format + newName;
            vipImage.setLegal(true);
            vipImage.setIsPath(filePath);
            return vipImage;
        } catch (Exception e) {
            e.printStackTrace();
        }
        vipImage.setLegal(false);
        return vipImage;
    }

    /**
     * 上传多张贵宾厅图片
     * @param files
     * @param request
     * @return
     */
    @Override
    public VipImage importImgs(MultipartFile[] files, HttpServletRequest request) {
        List<String> imgPaths = new ArrayList<>();
        for (MultipartFile multipartFile : files) {
            VipImage vipImage = importImg(multipartFile, request);
            if (vipImage.isLegal()) {
                imgPaths.add(vipImage.getIsPath());
            } else {
                return vipImage;
            }
        }
        VipImage vipImage = new VipImage();
        vipImage.setLegal(true);
        vipImage.setIsPaths(imgPaths);
        return vipImage;
    }
}
